package whf.framework.util;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

/**
 * 计数器
 * @author wanghaifeng
 *
 */
public class AccessCounter {
	private final static int MAX_SIZE = Integer.MAX_VALUE;
	
	List<Counter> list = new ArrayList<Counter>();
	
	private static AccessCounter counter = new AccessCounter();
	
	private AccessCounter(){
		InputStream is = null;
		try{
			is = AccessCounter.class.getClassLoader().getResourceAsStream("counter.properties");
			if(is == null) return;
			Properties props = new Properties();
			props.load(is);
			for(Iterator keys = props.keySet().iterator(); keys.hasNext();){
				String key = keys.next().toString();
				String ymds[] = StringUtils.split(key,"-");
				int y = Integer.parseInt(ymds[0]);
				int m = Integer.parseInt(ymds[1]);
				int d = Integer.parseInt(ymds[2]);
				int c = Integer.parseInt(props.getProperty(key));
				list.add(new Counter(y,m,d,c));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(is != null){
				try{
					is.close();
				}catch(Exception ignore){
					//
				}
			}
		}
	}
	
	public static AccessCounter getCounter(){
		return counter;
	}
	
	public static void inc(){
		getCounter().increace();
	}
	
	public void increace(){
		int[] ymd = this.getYMD();
		int year = ymd[0];
		int month = ymd[1];
		int day = ymd[2];
		Counter c = null;
		for(int i=0;i<list.size(); i++){
			Counter tmp = (Counter)list.get(i);
			if(tmp.year == year && tmp.month == month && tmp.day == day){
				c = tmp;
				break;
			}
		}
		if(c == null){
			this.save();
			c = new Counter(year, month, day);
			list.add(c);
		}
		c.inc();
		if(list.size()>MAX_SIZE){
			list.remove(0);
		}
	}
	
	private void save(){
		if(list.size() == 0) return;
		OutputStream os = null;
		try{
			String fileName = AccessCounter.class.getClassLoader().getResource("counter.properties").toString();
			int index = fileName.indexOf("/") +1;
			fileName = fileName.substring(index);
			Properties props = new Properties();
			for(int i=0;i<list.size(); i++){
				Counter c = (Counter)list.get(i);
				props.put(c.year+"-"+c.month+"-"+c.day, String.valueOf(c.count));
			}
			os = new FileOutputStream(fileName);
			props.save(os,"#");
		}catch(Exception ignore){
			ignore.printStackTrace();
		} finally{
			if(os != null){
				try{
					os.close();
				}catch(Exception ige){
					//
				}
			}
		}
	}
	
	public int getCount(int ymd[]){
		Counter c = null;
		for(int i=0;i<list.size(); i++){
			Counter tmp = (Counter)list.get(i);
			if(tmp.year == ymd[0] && tmp.month == ymd[1] && tmp.day == ymd[2]){
				c = tmp;
				break;
			}
		}
		if(c != null){
			return c.count;
		} else {
			return 0;
		}
	}
	
	private int[] getYMD(){
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		return new int[]{year, month, day};
	}
	
	public int getCount(){
		return getCount(getYMD());
	}
	
	class Counter{
		int year,month, day;
		int count;
		Counter(int year, int month, int day){
			this.year = year;
			this.month = month;
			this.day = day;
		}
		Counter(int year, int month, int day, int count){
			this.year = year;
			this.month = month;
			this.day = day;
			this.count = count;
		}
		void inc(){
			count ++;
		}
	}
	
	public static void main(String[] args) throws Exception{
		AccessCounter.inc();
		AccessCounter.getCounter().save();
	}
}
